<template>
  <div class="config-page">
    <h4 class="zl-title f-mt0">配置集列表
      <div class="fr">
        <search placeholder="请输入配置集名称" @change="searchConfig"></search>
        <Button type="primary" @click="configAddModify()" v-if="hasPermission($store, $route.params.environmentId, 'CONFIGMAPS_POST_ENV')">
          <Icon type="md-add" /> 添加配置集
        </Button>
      </div>
    </h4>
    <div class="zl-page-loading" v-if="isLoading">
      <Spin fix></Spin>
    </div>
    <Table v-else :columns="columns" :data="configList" class="config-table"></Table>

    <!-- 新增、修改 -->
    <add-modify-model :visible.sync="configVisible" :form="configForm" @finish="init" ></add-modify-model>
    <!-- 详情 -->
    <detail-model :visible.sync="detailVisible" :form="configForm"></detail-model>
  </div>
</template>
<script type="text/javascript">
import Fetch from '@/api/request'
import AddModifyModel from '@/components/config-set/add-modify-model'
import DetailModel from '@/components/config-set/detail-model'
import Search from '@/components/search'

export default {
  components: { AddModifyModel, DetailModel, Search },
  data() {
    return {
      columns: [{
        type: 'index',
        width: 60
      }, {
        title: '名称',
        key: 'id'
      }, {
        title: '创建时间',
        key: 'createTime'
      }, {
        title: '操作',
        render: (h, params) => {
          let btnList = [h('div', {
            class: 'button-inner',
            on: {
              click: () => {
                this.configDetail(params.row)
              }
            }
          }, [
            h('Icon', {
              props: {
                type: 'ios-paper'
              }
            }, ''),
            '详情'
          ])]
          if (this.hasPermission(this.$store, this.$route.params.environmentId, 'CONFIGMAPS_PUT_ENV|CONFIGMAP')) {
            btnList.push(h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.configAddModify(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'ios-create-outline'
                }
              }, ''),
              '编辑'
            ]))
          }
          if (this.hasPermission(this.$store, this.$route.params.environmentId, 'CONFIGMAPS_DELETE_ENV|CONFIGMAP')) {
            btnList.push(h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.configDelete(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'md-trash'
                }
              }, ''),
              '删除'
            ]))
          }

          return h('div', btnList)
        }
      }],
      configList: [],
      configListOrigin: [],
      configForm: {},
      configVisible: false,
      detailVisible: false,
      isLoading: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 根据名称搜索
    searchConfig(name) {
      let temp = []
      if (name) {
        this.configListOrigin.forEach((config) => {
          if (config.id.indexOf(name) !== -1) {
            temp.push(config)
          }
        })
      } else {
        temp = this.configListOrigin
      }
      this.configList = temp
    },
    // 删除配置集
    configDelete(config) {
      this.$Modal.confirm({
        title: '系统提示',
        content: '确定删除配置集（' + config.id + '）?',
        loading: true,
        onOk: () => {
          Fetch.delete({
            url: '/api/environments/' + this.$route.params.environmentId + '/configmaps/' + config.id,
            done: () => {
              this.$Modal.remove()
            }
          }).then((res) => {
            this.$Message.success({
              content: '删除配置集（' + config.id + '）成功！',
              duration: this.DURATION
            })
            this.init()
          })
        }
      })
    },
    // 配置集详情
    configDetail(config) {
      this.configForm = Object.assign({}, config)
      this.detailVisible = true
    },
    // 配置集新增、修改
    configAddModify(config) {
      if (config) {
        this.configForm = Object.assign({environmentName: config.environmentId}, config)
      } else {
        this.configForm = {
          environmentName: '',
          id: ''
        }
      }
      this.configVisible = true
    },
    // 获取配置集初始数据
    init() {
      Fetch.get({
        url: '/api/environments/' + this.$route.params.environmentId + '/configmaps',
        done: () => {
          this.isLoading = false
        }
      }).then((res) => {
        this.configList = res.data
        this.configListOrigin = res.data
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.config-page {
  .config-table {
    margin-top: 20px;
  }
  .zl-title {
    margin-top: 10px;
  }
}
</style>
